package com.oepm.web.controller;

import com.oepm.biz.ScoreBiz;
import com.oepm.common.entity.Meta;
import com.oepm.common.entity.Result;
import com.oepm.common.utils.JWTUtil;
import com.oepm.dao.entity.QueryInfo;
import com.oepm.dao.entity.ScorePo;
import com.oepm.dao.entity.TeacherDemoApplicationPo;
import com.oepm.dao.entity.vo.UploadScoreVo;
import com.oepm.vo.DataVo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@CrossOrigin
@RestController
public class ScoreController {
    @Autowired
    ScoreBiz scoreBiz;

    /**
     * 成绩列表
     * @return
     */
    @GetMapping("/score/select")
    public Result selectAll(QueryInfo queryInfo){
        DataVo dataVo = scoreBiz.selectAll(queryInfo);
        Result result = new Result(Meta.SUCCESS, dataVo);
        return result;
    }

    /**
     * 单个搜索查询
     * @param pname
     * @return
     */
    @GetMapping("/score/selectId/{pname}")
    public Result selectByName(@PathVariable String pname){
        List<ScorePo> scorePos = scoreBiz.selectByName(pname);
        Result result;
        if (scorePos!=null){
            result = new Result(Meta.SUCCESS,scorePos);
        }else{
            result = new Result(Meta.FAIL);
        }
        return result;
    }

    /**
     * 实验中心主任录入分数对应学院列表
     * @param request
     * @return
     */
    @GetMapping("/scoreUpload/select")
    @RequiresRoles(logical = Logical.OR, value = {"1","2"})
    public Result selectByAcademy(QueryInfo queryInfo,HttpServletRequest request){
        //获取token
        String token=request.getHeader("Authorization");
        //获取roled
        String roleId = JWTUtil.getRoleId(token);
        String academy = JWTUtil.getAcademy(token);

        if(roleId.equals("2")){
            //普通项目用实验室主任修改
            DataVo dataVo = scoreBiz.selectByAcademy(queryInfo,academy,"普通");
            Result result = new Result(Meta.SUCCESS, dataVo);
            return result;
        }else {
            DataVo dataVo = scoreBiz.selectByAcademys(queryInfo,"重点");
            Result result = new Result(Meta.SUCCESS, dataVo);
            return result;
        }
    }

    /**
     * 跟新分数
     * @param score
     * @return
     */
    @PostMapping("/scoreUpload/update")
    @RequiresRoles(logical = Logical.OR,value = {"1","2"})
    public Result updateScore(@RequestBody UploadScoreVo score){
        int flag = scoreBiz.updateScore(score);
        Result result;
        if (flag>0){
            result = new Result(Meta.SUCCESS);
        }else{
            result = new Result(Meta.FAIL);
        }
        return result;
    }

    @GetMapping("scoreUpload/selectId/{pid}")
    @RequiresRoles(logical = Logical.OR,value = "2")
    public Result selectByPid(@PathVariable String pid){
        UploadScoreVo uv = scoreBiz.selectByPid(pid);
        ArrayList<UploadScoreVo> list = new ArrayList<>();
        list.add(uv);
        Result result = new Result(Meta.SUCCESS,list);
        return result;
    }
}